import java.util.*;

class Team {
    String name;
    int matchesPlayed;
    int wins;
    int draws;
    int losses;
    int goalsFor;
    int goalsAgainst;
    int points;

    Team(String name) {
        this.name = name;
        this.matchesPlayed = 0;
        this.wins = 0;
        this.draws = 0;
        this.losses = 0;
        this.goalsFor = 0;
        this.goalsAgainst = 0;
        this.points = 0;
    }

    void updateStats(int scored, int conceded) {
        matchesPlayed++;
        goalsFor += scored;
        goalsAgainst += conceded;
        if (scored > conceded) {
            wins++;
            points += 3;
        } else if (scored < conceded) {
            losses++;
        } else {
            draws++;
            points += 1;
        }
    }

    @Override
    public String toString() {
        return String.format("%-15s %d %d %d %d %d %d %d", name, matchesPlayed, wins, draws, losses, goalsFor, goalsAgainst, points);
    }
}

public class TestRuanjian {
    static List<Team> teams = new ArrayList<>();

    public static void main(String[] args) {
        teams.add(new Team("球队1"));
        teams.add(new Team("球队2"));
        teams.add(new Team("球队3"));
        teams.add(new Team("球队4"));

        Scanner scanner = new Scanner(System.in);
        while (true) {
            System.out.println("输入比赛结果 (主队 客队 主队得分 客队得分)，输入exit退出:");
            String input = scanner.nextLine();
            if (input.equalsIgnoreCase("exit")) break;
            String[] parts = input.split(" ");
            String homeTeamName = parts[0];
            String awayTeamName = parts[1];
            int homeScore = Integer.parseInt(parts[2]);
            int awayScore = Integer.parseInt(parts[3]);

            Team homeTeam = findTeam(homeTeamName);
            Team awayTeam = findTeam(awayTeamName);
            if (homeTeam != null && awayTeam != null) {
                homeTeam.updateStats(homeScore, awayScore);
                awayTeam.updateStats(awayScore, homeScore);
                sortTeams();
                displayTable();
            } else {
                System.out.println("未找到球队，请检查输入。");
            }
        }
        scanner.close();
    }

    static Team findTeam(String name) {
        for (Team team : teams) {
            if (team.name.equals(name)) {
                return team;
            }
        }
        return null;
    }

    static void sortTeams() {
        teams.sort((t1, t2) -> {
            if (t1.points != t2.points) return t2.points - t1.points;
            int goalDifference1 = t1.goalsFor - t1.goalsAgainst;
            int goalDifference2 = t2.goalsFor - t2.goalsAgainst;
            if (goalDifference1 != goalDifference2) return goalDifference2 - goalDifference1;
            return t2.goalsFor - t2.goalsFor;
        });
    }

    static void displayTable() {
        System.out.printf("%-15s %s %s %s %s %s %s %s\n", "队名", "场数", "胜", "平", "负", "进球", "失球", "积分");
        for (Team team : teams) {
            System.out.println(team);
        }
    }
}
